Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize Value parser on role of the Value being parsed: transaction output or minting policy #666

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

smelc
Copy link
Contributor

@smelc smelc commented Oct 30, 2024

Changelog

- description: |
    Parameterize Value parser on role of the Value being parsed: transaction output or minting policy
# uncomment types applicable to the change:
  type:
  # - feature        # introduces a new feature
  - breaking       # the API has changed in a breaking way
  # - compatible     # the API has changed but is non-breaking
  # - optimisation   # measurable performance improvements
  # - refactoring    # QoL changes
  # - bugfix         # fixes a defect
  # - test           # fixes/modifies tests
  # - maintenance    # not directly related to the code
  # - release        # related to a new release preparation
  # - documentation  # change in code docs, haddocks...

Context

How to trust this PR

  1. This PR adds more checks
  2. Existing tests still pass (and there are a number of them!)

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • Self-reviewed the diff

import Control.Monad (when, unless)

data ValueRole
= RoleTxOut
Copy link
Contributor

@carbolymer carbolymer Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering about this usage site: https://github.com/IntersectMBO/cardano-cli/blob/447fa87001663f942a8f79252876b355fa1f0a04/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs#L2046

It's a parser of a whole utxo value, so it's not a Tx out value. It should work with RoleTxOut case of the parser, but the name should be different I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carbolymer> What do you prefer:

  1. Rename RoleTxOut into RoleTxOutOrUtxo
  2. Add a RoleUtxo

I'm tempted by option 2., in which case we could check that the quantity is positive and the only AssetId is ada?

Copy link
Contributor

@carbolymer carbolymer Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm liking the second option.

the only AssetId is ada?

UTXO can contain also non-ada, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carbolymer> yes I checked and UTxO can contain non-ada. So I added the new RoleUTxO but keps the same checks as RoleTxOut.

Copy link
Contributor

@carbolymer carbolymer Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that duplicated role makes sense here. You will always be able to put things which are in UTXO into TX output, so the format for the value will be the same.

I think I would just stick with RoleUTxO, since all tx outputs will be "unspent".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carbolymer> Only kept RoleUTxO 👍

@smelc smelc force-pushed the smelc/split-value-parser branch 2 times, most recently from 64d655c to ed2d380 Compare October 31, 2024 10:44
Copy link
Contributor

@carbolymer carbolymer Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 67, there's a case match:

ValueExprMultiAsset polId aName quant -> [(AssetId polId aName, quant)]

could you provide a parser

Parser (PolicyId, AssetName, Quantity)

?

(It could be also Parser [(PolicyId, AssetName, Quantity)] - whatever is more convenient here).

That would be very useful for parsing values for minting.

This can be done in a follow up PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't plan to work on it, I can implement that after you finish your work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carbolymer> I want to have @Jimbo4350's feedback before amending this PR.

Copy link
Contributor

@Jimbo4350 Jimbo4350 Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of a change like this: master...jordan/delete-me-value-parser-test

ghci> parse parseMintingMultiAssetValue "" "-5 51936f3c98a04b6609aa9b5c832ba1182cf43a58e534fcc05db09d69.4D696C6C6172436F696E"
Right (valueFromList [(AssetId "51936f3c98a04b6609aa9b5c832ba1182cf43a58e534fcc05db09d69" "MillarCoin",-5)])
ghci> parse  parseTxOutMultiAssetValue "" "-5 51936f3c98a04b6609aa9b5c832ba1182cf43a58e534fcc05db09d69.4D696C6C6172436F696E"
Left (line 1, column 1):
unexpected "-"
expecting multi-asset value expression

We could customize the error message if need be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jimbo4350> the problem with the approach consisting of restricting the operators is that it forbids sums whose total is positive, but that contain negative operands (as tested in this test: https://github.com/IntersectMBO/cardano-cli/blob/43137957a2e943bf9873718f1c99591e8b54afd0/cardano-cli/test/cardano-cli-test/Test/Cli/Shelley/Transaction/Build.hs#L93).

I checked with Modus Create's smart contracts auditing team and they reported that they see such uses.

So I kept my approach but added the new functions parseTxOutMultiAssetValue and parseMintingMultiAssetValue that you had shown.

Copy link
Contributor

@carbolymer carbolymer Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jimbo4350

parseMintingMultiAssetValue :: Parser Value

My main problem with the parser is it's return type. Value is too broad for minting - you shouldn't be able to keep ADA there.

Copy link
Contributor

@carbolymer carbolymer Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding negative values in minting, I think we still need them for burning tokens. https://developers.cardano.org/docs/native-tokens/minting/#burning-token

case role of
RoleUTxO -> do
unless (allPositive value) $
fail "Value must be positive in an UTxO (or transaction output)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would return the value as well in the error messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Showed the value 👍

@@ -52,12 +52,12 @@ parseValue role = do
   case role of
     RoleUTxO -> do
       unless (allPositive value) $
-        fail "Value must be positive in an UTxO (or transaction output)"
+        fail $ "Value must be positive in UTxO (or transaction output): " <> show value
       return value
     RoleMint -> do
       let (Coin lovelace) = selectLovelace value
       when (lovelace /= 0) $
-        fail "Lovelace must be zero in a minting value"
+        fail $ "Lovelace must be zero in minting value: " <> show value
       return value

@smelc smelc enabled auto-merge November 6, 2024 10:02
@smelc smelc added this pull request to the merge queue Nov 6, 2024
Merged via the queue into master with commit c8b3d83 Nov 6, 2024
25 checks passed
@smelc smelc deleted the smelc/split-value-parser branch November 6, 2024 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants